package com.woniu.cms.client.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.client.core.entity.ClientComplaint;
import com.woniu.cms.client.core.entity.ClientInfo;
import com.woniu.cms.client.core.entity.ClientNegotiation;
import com.woniu.cms.client.core.service.IClientInfoService;
import com.woniu.cms.client.core.service.IClientNegotiationService;
import com.woniu.cms.client.core.service.impl.ClientNegotiationServiceImpl;
import com.woniu.cms.client.core.vo.ClientComplaintVo;
import com.woniu.cms.client.core.vo.ClientInfoVo;
import com.woniu.cms.client.core.vo.ClientNegotiationVo;
import com.woniu.common.api.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenjiapeng
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/client-negotiation")
public class ClientNegotiationController {


    @Autowired
    private IClientNegotiationService   iClientNegotiationService;

    @Autowired
    private IClientInfoService iClientInfoService;


    @Transactional
    @ApiOperation("保存客户投诉信息")
    @PostMapping
    public Result saveClientNegotiation(@RequestBody ClientNegotiation clientNegotiation){

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientNegotiation.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientNegotiation.setInfoId(clientInfo.getId());

        boolean save = iClientNegotiationService.save(clientNegotiation);
        return save ? Result.ok() :Result.error();
    }


    @ApiOperation("根据id删除客户投诉信息")
    @DeleteMapping("/{id}")
    public Result deleteClientNegotiation(@PathVariable Integer id){

        boolean b = iClientNegotiationService.removeById(id);

        return b? Result.ok() :Result.error();

    }

    @ApiOperation("批量删除客户投诉信息")
    @DeleteMapping
    public Result deleteClientNegotiations(@RequestBody List<Integer> ids){
        //批量删除ClientComplaint
        boolean b = iClientNegotiationService.removeBatchByIds(ids);
        return b? Result.ok() :Result.error();
    }

    /*这个地方可以再优化一下，然后可以加个非空的注解，确保前端传进来的的值是有值的*/
    @Transactional
    @ApiOperation("修改投诉信息")
    @PutMapping
    public Result updateClientNegotiation(@RequestBody ClientNegotiation clientNegotiation){

        QueryWrapper<ClientInfo> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("client_name",clientNegotiation.getClientName());

        ClientInfo clientInfo = iClientInfoService.getOne(queryWrapper);

        clientNegotiation.setInfoId(clientInfo.getId());

        boolean save = iClientNegotiationService.updateById(clientNegotiation);
        return save ? Result.ok() :Result.error();
    }



    @ApiOperation(value = "通过客户列表Id查找客户沟通信息")
    @GetMapping("{id}")
    public  Result findById(@PathVariable  Integer id) {

        QueryWrapper<ClientNegotiation> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("info_id",id);

        Map<String, Object> clientNegotiations = iClientNegotiationService.getMap(queryWrapper);

        return Result.common(clientNegotiations);
    }


    @GetMapping("all")
    @ApiOperation(value = "客户沟通信息的分页的查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前的页数",example = "1"),
            @ApiImplicitParam(name = "pageSize",value = "分页大小",example = "3"),
    })
    public Result pageClientNegotiation(Integer currentPage, int pageSize){

        IPage<ClientNegotiation> ClientNegotiationIPage = new Page(currentPage,pageSize);

        IPage<ClientNegotiation> page = iClientNegotiationService.page(ClientNegotiationIPage);

        System.out.println("page = " + page);

        List<ClientNegotiation> records = page.getRecords();

        System.out.println("records = " + records);


        List<ClientNegotiationVo> clientNegotiationVos = copyListProperties(records);

    /*    records.forEach(e->{

            ClientNegotiationVo clientNegotiationVo = new ClientNegotiationVo();
            BeanUtils.copyProperties(e,clientNegotiationVo);

        });
*/
        long total = page.getTotal();

        Map<String,Object> map = new HashMap<>();
        map.put("data",clientNegotiationVos);
        map.put("total",total);

        return Result.common(map);

    }


    private  List<ClientNegotiationVo>  copyListProperties(List<ClientNegotiation> records){

        List<ClientNegotiationVo> clientNegotiationVos  = MyBeanUtils.copyListProperties(records, ClientNegotiationVo::new);

        return clientNegotiationVos;

    }



}

